package cn.cvs.controller;

import cn.cvs.pojo.TSupplier;
import cn.cvs.pojo.TSysUser;
import cn.cvs.service.SupplierService;
import cn.cvs.utils.Constants;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/sys/supplier")
public class SupplierController extends BaseController{
    @Resource
    SupplierService supplierService;

    @RequestMapping("/toAdd")
    public String toAdd(){
        System.out.println("----------------toAdd");
        return "supplier/add";
    }

    @RequestMapping("/add")
    public String add(TSupplier tSupplier
                            , HttpServletRequest request
                            ,@RequestParam(value = "attachs" ,required = false) MultipartFile[] attachs
                      ){
        System.out.println("----------------add");
        try {
            //添加 普通字段
            //取出Session中当前登录的用户的id,set到createdUserId中
            tSupplier.setCreatedUserId(((TSysUser)request.getSession().getAttribute(Constants.userSession)).getId());

            System.out.println(tSupplier);
            if(supplierService.addSupplier(tSupplier)){
                //错误示范--start
//                return "list"; //成功   "list"->WEB-INF/jsp/list.jsp
                //转发     url不变
                //重定向  url改变
                //return "/list"; //成功  /sys/supplier/add  添加页面的请求路径
                //                              sys/supplier/
                //错误示范--end

                //如果是在同一个controller 进行页面的跳转 redirect:list  ,不要/
                //return  "redirect:list";  //sss_cvs317/list  重定向， url会改变
                //如果是不同的 controller 进行页面的跳转   sys/supplier/  -->   sys/user

                //redirect 加了 /  --》 http://localhost:8080/ssm_cvs317/
                return  "redirect:/sys/supplier/list";  //sss_cvs317/list  重定向， url会改变
            }else{
                request.setAttribute("error","添加失败");
                return "supplier/add";  //失败
            }

        } catch (Exception exception) {
            exception.printStackTrace();
            request.setAttribute("error","添加失败"+ exception.getMessage());
            return "error";  //失败
        }

    }

    @RequestMapping("/list")
    public String list(Model model
                        , @RequestParam(value = "querySupCode" ,required = false) String querySupCode
                        , @RequestParam(value = "querySupName" ,required = false) String querySupName
                        , @RequestParam(value = "pageIndex",defaultValue = "1",required = false) String pageIndex
                ){
        System.out.println("----------------list");
        //分页
        Integer _pageIndex = Integer.valueOf(pageIndex)<=0?1: Integer.valueOf(pageIndex);
        PageHelper.startPage(_pageIndex, Constants.navigatePages);
        //按原有的逻辑查询
        TSupplier params = new TSupplier(querySupCode,querySupName);
        List<TSupplier> supplierList = supplierService.getSupplierByPOJO(params);
        //查询结果存入PageInfo对象中
        PageInfo<TSupplier> pageInfo =new PageInfo<>(supplierList);

        //model.addAttribute("supplierList",supplierList);
        model.addAttribute("pageInfo",pageInfo);
        return "supplier/list";
    }

    @ResponseBody
    @RequestMapping(value = "/{id}/view")////,produces = {"application/json;charset=UTF-8"})
    public Object view(@PathVariable String id){
        System.out.println("----------------view: id : "+ id);
        TSupplier params = new TSupplier(Integer.valueOf(id));
        List<TSupplier> supplierList = supplierService.getSupplierByPOJO(params);

        if (null!=supplierList &&  supplierList.size()>0){
          //  return JSON.toJSONString(supplierList.get(0)==null?"nodata":supplierList.get(0));// supplierList.get(0)==null?"nodata":supplierList.get(0);
            return supplierList.get(0)==null?"nodata":supplierList.get(0);
        }


        return "supplier/view";
    }

}
